import ReactDOM from 'react-dom';
import { Loading } from './index';

let needLoadingRequestCount = 0;

/**
 * @description Show Loading
 */
export const showFullScreenLoading = () => {
  if (needLoadingRequestCount === 0) {
    const dom = document.createElement('div');
    dom.setAttribute('id', 'loading');
    document.body.appendChild(dom);
    ReactDOM.render(Loading(), dom);
  }
  needLoadingRequestCount++;
};

/**
 * @description Hide Loading
 */
export const tryHideFullScreenLoading = () => {
  if (needLoadingRequestCount <= 0) return;
  needLoadingRequestCount--;
  if (needLoadingRequestCount === 0) {
    document.body.removeChild(
      document.getElementById('loading') as HTMLElement
    );
  }
};
